package edu.toronto.whimper;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
import edu.toronto.whimper.DataProcessor.DBAdapter;
import edu.toronto.whimper.DataProcessor.DBManager;
import edu.toronto.whimper.GeneralObjects.NoiseSample;
import edu.toronto.whimper.NoiseObjects.GPSReader;
import edu.toronto.whimper.NoiseObjects.NoiseMeter;


public class Main extends Activity {
	
	public static DBAdapter mainDBNoiseSamples = null;
	
	public static NoiseMeter mainNoiseMeter = null;
	public static GPSReader  mainGPSReader = null;
	
	public static Context mainContext;
	
	//region: static fields
	public static Date today;
	
	public static Date getDate()
    {
       return today;
    }   
	  
	public static void setDate(Date d)
	{
		today = d;
	}  
	//end static fields
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        getTodaysDate();
        mainContext = this;
        
        mainDBNoiseSamples = new DBAdapter(this); 
        
        //Utils.LoadToDb(this);
        //DBManager.insertDumpNoiseSample(mainDBNoiseSamples);
        //viewData();
        
        mainGPSReader = new GPSReader(this);
        mainNoiseMeter = new NoiseMeter();
        mainNoiseMeter.measureStart();
   }
    
    @Override
    protected void onDestroy() {
    	mainNoiseMeter.measureStop();
    	super.onDestroy();
    }
    
    @SuppressWarnings("unused")
	private void viewData()
    {
    	ArrayList<NoiseSample> allSamples = DBManager.getAllNoiseSamples(mainDBNoiseSamples);
    	
    	Toast.makeText( getApplicationContext(), "There are " + allSamples.size() + " samples.", Toast.LENGTH_SHORT).show();
    	
    	/*for (NoiseSample var : allSamples) 
    	{
    		String Text = var.toString();    		
    		Toast.makeText( getApplicationContext(), Text, Toast.LENGTH_SHORT).show();
    	}*/
    	
    }    
    

    public void mainmenuButtonClickHandler(View clickedButton){
    	switch(clickedButton.getId()){
    	case R.id.noiseMap:
    		Intent noiseMapIntent = new Intent(Main.this, NoiseMap.class);
    		startActivity(noiseMapIntent);
    		break;
    	case R.id.soundMeter:
    		Intent noiseLevelIntent = new Intent(Main.this, NoiseLevel.class);
    		startActivity(noiseLevelIntent);
    		break;
    	case R.id.noiseExposure:
    		//Toast.makeText(this, "click", Toast.LENGTH_SHORT).show();
    		Intent noiseExposureIntent = new Intent(Main.this, NoiseExposure.class);
    		startActivity(noiseExposureIntent);
    		break;	
    	case R.id.buttonExit:
    		//save data to file
            Utils.checkExternalStorageAvailability();
		    if(Utils.mExternalStorageAvailable && Utils.mExternalStorageWriteable)
		    {
	    		DBAdapter db = new DBAdapter(this);
	    		ArrayList<NoiseSample> allSamples = DBManager.getAllNoiseSamples(db);
	    		Utils.SaveToFile(this, allSamples);
		    }
    		Builder builder = new AlertDialog.Builder(this);
			builder.setMessage("Are you sure you want to quit?")
			.setCancelable(true)
			.setPositiveButton("Yes",
					new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog,
								int which) {
							// Ends the activity and exit from the program
							Intent intent = new Intent(Intent.ACTION_MAIN);
				        	intent.addCategory(Intent.CATEGORY_HOME);
				        	intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
				        	startActivity(intent);
							//Main.this.finish();
						}
					})
			.setNegativeButton("No",
					new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog,
								int which) {
							Toast.makeText(getApplicationContext(),
									"Returning to the program",
									Toast.LENGTH_SHORT).show();
						}
					});
			AlertDialog exitDialog = builder.create();
			exitDialog.show();
    		break;
    	}
    }
    
    private void getTodaysDate()
    {
     	//geting today's date
    	    String todayStr  = Utils.getTodaysDate();
    	    setDate(Utils.getDateTimeFromStr(todayStr, new SimpleDateFormat("yyyy/MM/dd")));    	     
    }

}